<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Property extends Model_Base {

    protected $_name = 'property';
    protected $_primaryKey = 'prp_id';

    /**
     *  This function is used to get Questions
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchProperty($where = "", $limit = "", $offset = "0", $order = 'prp_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('prp' => 'property'))
                ->setIntegrityCheck(false)
                ->join(array('m' => 'members'), "prp.member_id = m.member_id")
                ->join(array('md' => 'members_details'), "prp.member_id = md.member_id");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        
        //echo $select; exit;
        
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array('' => '-- Select Property type --');
        }
    }
    
    public function fetchPropertyListing($where = "", $limit = "", $offset = "0", $order = 'prp_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('prp' => 'property'))
                ->setIntegrityCheck(false)
                ->join(array('m' => 'members'), "prp.member_id = m.member_id")
                ->join(array('prpty' => 'property_type'), "prpty.prtyp_id = prp.property_type")
                ->join(array('ctr' => 'country'), "ctr.country_id = prp.prp_country")
                ->join(array('ct' => 'city'), "ct.city_id = prp.prp_city")
                ->join(array('md' => 'members_details'), "prp.member_id = md.member_id");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        
        //echo $select; exit;
        
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array('' => '-- Select Property type --');
        }
    }
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('prp' => 'property'))
                ->setIntegrityCheck(false)
                ->join(array('m' => 'members'), "prp.member_id = m.member_id")
                ->join(array('md' => 'members_details'), "prp.member_id = md.member_id")
                ->where('prp_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    
    
    
    
    
        /**
     *  This function is used to get total orders
     * @return int
     */
    public function getPropertyCount($where) {
        
        $select = $this->select()
                ->from(array('prp' => 'property'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('m' => 'members'), "prp.member_id = m.member_id")
                ->join(array('prpty' => 'property_type'), "prpty.prtyp_id = prp.property_type")
                ->join(array('ctr' => 'country'), "ctr.country_id = prp.prp_country")
                ->join(array('ct' => 'city'), "ct.city_id = prp.prp_city")
                ->join(array('md' => 'members_details'), "prp.member_id = md.member_id");
        
        
        
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }


    
    

}
